import Foundation

enum APIEndpoint {
    case components

    var url: URL {
        let baseURL = "https://tf.cn"
        switch self {
        case .components:
            return URL(string: baseURL + "/components")!
        }
    }
}

protocol APIServiceProtocol {
    func fetch(url: URL, method: String, body: Data?) async throws -> Data
}

class API: APIServiceProtocol {
    static let shared = API()

    private init() {}

    func fetch(url: URL, method: String = "GET", body: Data? = nil) async throws -> Data {
        return Data()
    }
}

protocol DataService {
    func fetchData<T: Decodable>(from endpoint: APIEndpoint) async throws -> T
}

class DataServiceImp: DataService {
    private let apiService: APIServiceProtocol

    init(apiService: APIServiceProtocol = API.shared) {
        self.apiService = apiService
    }

    func fetchData<T: Decodable>(from endpoint: APIEndpoint) async throws -> T {
        let url = endpoint.url
        let data = try await apiService.fetch(url: url, method: "POST", body: nil)
        return try JSONDecoder().decode(T.self, from: data)
    }
}
